<template>
	<view>
		<!-- 搜索跳转 -->
		<view class="search-box">
			<my-search @click="gotoSearch"></my-search>
		</view>


		<!-- 轮播图 -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
			<swiper-item v-for="(item, i) in swiperList" :key="item.goods_id">
				<navigator class="swiper-item" :url="`/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`">
					<image :src="item.image_src"></image>
				</navigator>
			</swiper-item>
		</swiper>

		<!-- 分类导航 -->
		<view class="nav-list">
			<view class="nav-item" v-for="(item, i) in navList" :key="item.name" @click="navClickHandler(item)">
				<image class="nav-img" :src="item.image_src"></image>
			</view>
		</view>

		<!-- 楼层数据 -->
		<view class="floor-list">
			<view class="floor-item" v-for="(item, i) in floorList" :key="i">
				<!-- 楼层title 图片 -->
				<image class="floor-title" :src="item.floor_title.image_src"></image>

				<!-- 楼层图片 -->
				<view class="floor-img-box">
					<!-- 左侧的大图 -->
					<navigator class="left-img-box" :url="item.product_list[0].url">
						<image :src="item.product_list[0].image_src"
							:style="{ width: item.product_list[0].image_width +'rpx'}" mode="widthFix"></image>
					</navigator>
					<!-- 右侧小图-列表渲染 -->
					<view class="right-img-box">
						<navigator class="right-img-item" v-for="(item2, i2) in item.product_list" v-if="i2 >0"
							:url="item2.url" :key="i2">
							<image :src="item2.image_src" :style="{width: item2.image_width +'rpx'}" mode="widthFix">
							</image>
						</navigator>
					</view>
				</view>


			</view>
		</view>

	</view>
</template>

<script>
	import badgeMix from '@/mixins/tabbar-badge.js'

	export default {
		// 混入
		mixins: [badgeMix],
		data() {
			return {
				// 轮播图数据
				swiperList: [],
				// 分类导航数据
				navList: [],
				// 获取楼层数据
				floorList: [],
			};
		},
		// 在 onLoad 声明周期函数发起网络请求
		onLoad() {
			// 发起网络请求
			this.getSwiperList()
			this.getNavList()
			this.getFloowList()
		},
		methods: {
			// 请求轮播图数据
			async getSwiperList() {
				console.log('获取数据')
				// const result = await uni.$http.get('/home/swiperdata')
				// const res = result.data
				// 对象的解构赋值，和上面的代码等效
				const {
					data: res
				} = await uni.$http.get('/home/swiperdata')
				console.log('res', res)
				// 数据请求异常的判断
				if (res.meta.status !== 200) return uni.$showMsg()
				// if (res.meta.status !== 200) {
				// 	return uni.showToast({
				// 		title: '数据请求失败！',
				// 		duration: 1500,
				// 		icon: 'none',
				// 	})
				// }
				// 设置页面数据
				this.swiperList = res.message
			},

			// 请求分类数据
			async getNavList() {
				const {
					data: res
				} = await uni.$http.get('/home/catitems')
				// 数据请求异常的判断
				if (res.meta.status !== 200) return uni.$showMsg()
				console.log('res2', res)
				// 设置页面数据
				this.navList = res.message
			},

			// 请求楼层数据
			async getFloowList() {
				const {
					data: res
				} = await uni.$http.get('/home/floordata')
				if (res.meta.status !== 200) return uni.$showMsg()
				console.log('res3', res)
				// 数据格式处理
				res.message.forEach(item => {
					item.product_list.forEach(prod => {
						let query = prod.navigator_url.split('?')[1]
						prod.url = '/subpkg/goods_list/goods_list?' + query
					})
				})

				this.floorList = res.message
			},


			// 点击分类跳转到分类tab页面
			navClickHandler(item) {
				console.log(item)
				if (item.name === '分类') {
					// tabBar 页面跳转
					uni.switchTab({
						url: '/pages/cate/cate'
					})
				}
			},

			gotoSearch() {
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			}

		}
	}
</script>

<style lang="scss">
	.search-box {
		position: sticky;
		top: 0;
		// left: 0;
		z-index: 1;
	}


	swiper {
		height: 330rpx;

		.swiper-item,
		image {
			width: 100%;
			height: 100%;
		}
	}

	.nav-list {
		display: flex;
		justify-content: space-around;
		margin: 32rpx 0;

		.nav-img {
			width: 128rpx;
			height: 140rpx;
		}
	}

	.floor-list {
		padding-left: 8rpx;

		.floor-item {
			// margin-left: 8rpx;

			.floor-title {
				height: 60rpx;
				width: 100%;
			}

			.floor-img-box {
				display: flex;

				.right-img-box {
					display: flex;
					flex-wrap: wrap;
					justify-content: space-around;
				}
			}
		}
	}
</style>
